﻿/*
 * Created by SharpDevelop.
 * User: phtrung
 * Date: 6/29/2009
 * Time: 2:36 PM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.IO;
using popLib;

namespace QuickBackup
{
	class Program
	{
		/// args[0]: directory name or file name or file extension (full path)
		/// args[1]: backup destination
		/// args[2]: back up type [/F] [/D]
		/// args[3]: options[/DT] [/T] [/D] [/C], no option mean override the file
		/// [/DT]: include date and time
		/// [/D]:  include date and overide file with same date
		/// [/T]:  include time only and overide file with same time (rarely)
		/// [/C]:  include the count number
		
		public static void Main(string[] args)
		{
			try{
			if( !File.Exists(FileLib.getFullPath("7z\\7z.exe")) ){
				if( !Directory.Exists(FileLib.getFullPath("7z")) ) Directory.CreateDirectory( FileLib.getFullPath("7z") );
				FormLib.extractEmbedResource( "QuickBackup.7z.7z.exe", FileLib.getFullPath("7z\\7z.exe") );
				FormLib.extractEmbedResource( "QuickBackup.7z.7z.dll", FileLib.getFullPath("7z\\7z.dll") );
			}
			}catch(Exception ex){FormLib.alert(ex);}
			
			if(args.Length >= 2){
				try{
					//for backup file
					if(args.Length == 2 || ( args.Length >= 3 && args[2].ToUpper() == "/F" ))
						CopyFile(args);
					
					//for backup directory
					if( args.Length >= 3 && args[2].ToUpper() == "/D" )
						CopyDirectory(args);
					
				}catch(Exception ex){
					System.Console.WriteLine(ex.ToString());
					System.Console.WriteLine("Press Enter to exit...");
					System.Console.ReadLine();
				};				
				
			}else {
				System.Console.WriteLine("QuickBackup.exe [FILE] [DEST] [TYPE] [OPTIONS]\r\n");
				System.Console.WriteLine("[FILE]: Directory name or file name or file extension (full path)" +
				                         "\r\n[DEST]: backup destination" +
				                         "\r\n[TYPE]: back up type [/F] [/D], not specific type mean file backup only" +
				                         "\r\n[OPTIONS]: [/DT] [/T] [/D] [/C], no option mean override the file" +
				                         "\r\n\t[/DT]: include date and time" +
				                         "\r\n\t[/D]:  include date and overide file with same date" +
				                         "\r\n\t[/T]:  include time only and overide file with same time (rarely)");// +
				                         //"\r\n\t[/C]:  include the count number");
				System.Console.WriteLine("Press Enter to exit...");
				System.Console.ReadLine();
			}
		}
		
		public static void CopyFile(string[] args){
			string subfix = "";
			if(args.Length >= 4){
				if(args[3].ToUpper() == "/DT") subfix = " "+ DateTime.Now.ToShortDateString().Replace("/","-") +" "+ DateTime.Now.ToShortTimeString().Replace(":","-");
				if(args[3].ToUpper() == "/D")  subfix = " "+ DateTime.Now.ToShortDateString().Replace("/","-");
				if(args[3].ToUpper() == "/T")  subfix = " "+ DateTime.Now.ToShortTimeString().Replace(":","-");				
			}
			
			if( args[0].IndexOf("*") >=0 ){
					string extension = ((args[0].IndexOf("\\")>=0) ? args[0].Split('\\')[args[0].Split('\\').Length - 1] : args[0] );
					string source    = ((args[0].IndexOf("\\")>=0) ? args[0].Replace(extension,"") : "." );
					string[] file    = Directory.GetFiles( source , extension );
					
					if(!Directory.Exists(args[1])) 	Directory.CreateDirectory(args[1]);
					
					for(int i=0;i<file.Length;i++){
						string filename 	= file[i].Split('\\')[ file[i].Split('\\').Length - 1 ];
						string sourcefile   = source + "\\" + filename;
						string destination 	= args[1] +"\\" + InsertSubfix(filename, subfix);
						 
						
						File.Copy( sourcefile,destination,true);
					}					
			}else{				 
				
				string destination = ((args[0].IndexOf("\\")>=0) ? args[0].Split('\\')[args[0].Split('\\').Length - 1] : args[0] );
				destination = args[1] + "\\" + InsertSubfix( destination, subfix );
				
				if(!Directory.Exists(args[1])) 	Directory.CreateDirectory(args[1]);
				
				File.Copy(args[0],destination,true);
			}
		}	
		
		
		public static void CopyDirectory(string[] args){			
			string subfix = "";
			if(args.Length >= 4){
				if(args[3].ToUpper() == "/DT") subfix = " "+ DateTime.Now.ToShortDateString().Replace("/","-") +" "+ DateTime.Now.ToShortTimeString().Replace(":","-");
				if(args[3].ToUpper() == "/D")  subfix = " "+ DateTime.Now.ToShortDateString().Replace("/","-");
				if(args[3].ToUpper() == "/T")  subfix = " "+ DateTime.Now.ToShortTimeString().Replace(":","-");				
			}
			
			if( args[0].IndexOf("*") >=0 ){					
				string extension 		= ((args[0].IndexOf("\\")>=0) ? args[0].Split('\\')[args[0].Split('\\').Length - 1] : args[0] );
				string source    		= ((args[0].IndexOf("\\")>=0) ? args[0].Replace(extension,"") : "." );
				string[] directories    = Directory.GetDirectories( source , extension );
				
				string oriDestination   = args[1];
				
				for(int i=0;i<directories.Length;i++){					
					string foldername 	= directories[i].Split('\\')[ directories[i].Split('\\').Length - 1 ];
					string sourcefile   = directories[i] + "\\*.*";
					string destination 	= oriDestination +"\\" + foldername + subfix;					
					
					args[0] = sourcefile;
					args[1] = destination;
					if(args.Length>=3) args[3] = "";
					CopyFile(args);
					
					args[0] = directories[i] + "\\*";
					CopyDirectory(args);
				}				
			}else{
				
				if(args[0] == ".") args[0] = Directory.GetCurrentDirectory();
				string sourceFolder   = ((args[0].IndexOf("\\")>=0) ? args[0].Split('\\')[args[0].Split('\\').Length - 1] : args[0] );
				string destination = args[1] + "\\" + sourceFolder + subfix;
				
				if(!Directory.Exists(destination)) 	Directory.CreateDirectory(destination);
				args[0] = args[0] + "\\*.*";
				args[1] = destination;
				
				if(args.Length>=3) args[3] = "";
				CopyFile(args);
				
				args[0] = args[0].Replace("*.*","*");
				CopyDirectory(args);
				
			}
		}
		
		public static string InsertSubfix(string filename,string subfix){
			if( filename.LastIndexOf(".") >= 0 && filename.LastIndexOf(".") > filename.LastIndexOf("\\") )
				return filename.Substring(0, filename.LastIndexOf(".") ) + subfix + filename.Substring(filename.LastIndexOf(".") );
			else return filename + subfix;
		}
		
	}
}
